#include "UtilityPCH.h"

#include "UT_TimeManager.h"
#include "UT_Assert.h"
#include "time.h"

using namespace Utility;
//----------------------------------------------------------------------------
//UT_TimeManager
//----------------------------------------------------------------------------
UT_TimeManager::UT_TimeManager()
{
	m_uiSystemTime = 0;
	m_kTimeLineIDPtrMap.Clear();
}
//----------------------------------------------------------------------------
UT_TimeManager::~UT_TimeManager()
{
	UT_TimeLineIDPtrMapIter IterBe = m_kTimeLineIDPtrMap.GetBegin();
	UT_TimeLineIDPtrMapIter IterEd = m_kTimeLineIDPtrMap.GetEnd();
	for(; IterBe != IterEd; IterBe++)
	{
		UT_TimeLine* pkTimeLine = IterBe->second;
		MM_SAFE_DELETE(pkTimeLine);
	}

	m_kTimeLineIDPtrMap.Clear();
}
//----------------------------------------------------------------------------
void UT_TimeManager::Init()
{
	UT_TimeLine* pkDefaultTimeLine = new UT_TimeLine;
	pkDefaultTimeLine->SetPlaySpeed(1.0f);

	RegisterTimeLine(0,pkDefaultTimeLine);
}
//----------------------------------------------------------------------------
void UT_TimeManager::RegisterTimeLine(uint8_t ucTimeLineID,UT_TimeLine* pkTimeLine)
{
	UT_TimeLineIDPtrMapIter IterFind = m_kTimeLineIDPtrMap.Find(ucTimeLineID);
	UT_ASSERT(IterFind != m_kTimeLineIDPtrMap.GetEnd());

	if(IterFind != m_kTimeLineIDPtrMap.GetEnd())
		return;

	m_kTimeLineIDPtrMap.GetAt(ucTimeLineID) = pkTimeLine;
}
//----------------------------------------------------------------------------
UT_TimeLine* UT_TimeManager::RequestTimeLine(uint8_t ucTimeLineID)
{
	UT_TimeLineIDPtrMapIter IterFind = m_kTimeLineIDPtrMap.Find(ucTimeLineID);
	if(IterFind == m_kTimeLineIDPtrMap.GetEnd())
		return NULL;
	else
		return IterFind->second;
}
//----------------------------------------------------------------------------
void UT_TimeManager::Update()
{
	UT_TimeLineIDPtrMapIter IterBe = m_kTimeLineIDPtrMap.GetBegin();
	UT_TimeLineIDPtrMapIter IterEd = m_kTimeLineIDPtrMap.GetEnd();
	for(; IterBe != IterEd; IterBe++)
	{
		UT_TimeLine* pkTimeLine = IterBe->second;
		if(NULL != pkTimeLine)
			pkTimeLine->Tick();
	}

	m_uiSystemTime = time(0);
}
//----------------------------------------------------------------------------
void UT_TimeManager::StartTimeLine( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return;

	pkTimeLine->StartTimeLine();
}
//----------------------------------------------------------------------------
void UT_TimeManager::StopTimeLine( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return;

	pkTimeLine->StopTimeLine();
}
//----------------------------------------------------------------------------
void UT_TimeManager::PauseTimeLine( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return;

	pkTimeLine->PauseTimeLine();
}
//----------------------------------------------------------------------------
void UT_TimeManager::PlayTimeLine( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return;

	pkTimeLine->PlayTimeLine();
}
//----------------------------------------------------------------------------
void UT_TimeManager::SetTimeLinePlaySpeed(uint8_t ucTimeLineID,float fSpeed)
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return;

	pkTimeLine->SetPlaySpeed(fSpeed);
}
//----------------------------------------------------------------------------
float Utility::UT_TimeManager::GetTimeLineCurrentScaleTime( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0.0f;

	return pkTimeLine->GetCurrentScaleTime();
}
//----------------------------------------------------------------------------
float UT_TimeManager::GetTimeLineDeltaScaleTime( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0.0f;

	return pkTimeLine->GetDeltaScaleTime();
}
//----------------------------------------------------------------------------
float UT_TimeManager::GetTimeLineCurrentRealTime( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0.0f;

	return pkTimeLine->GetCurrentRealTime();
}
//----------------------------------------------------------------------------
float Utility::UT_TimeManager::GetTimeLineDeltaRealTime( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0.0f;

	return pkTimeLine->GetDeltaRealTime();
}
//----------------------------------------------------------------------------
uint32_t UT_TimeManager::GetTimeLineState( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0;

	return pkTimeLine->GetTimeLineState();
}
//----------------------------------------------------------------------------
float UT_TimeManager::GetTimeLinePlaySpeed( uint8_t ucTimeLineID )
{
	UT_TimeLine* pkTimeLine = RequestTimeLine(ucTimeLineID);
	if(NULL == pkTimeLine)
		return	0;

	return pkTimeLine->GetPlaySpeed();
}